package com.lz.commonlibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Some method help do some UI works
 * User: cym
 * Date: 13-9-22
 *
 */
public class BasicUiUtils {
	
	/**
     * Edit是否可编辑
     * */

    public static void lockUnlock(boolean value, View[] views) {
        if (value) {
            for (int i = 0; i < views.length; i++) {
            	EnableView(views[i], true);
            }

        } else {
            for (int i = 0; i < views.length; i++) {
            	EnableView(views[i], false);
            }
        }
    }

    public static void EnableView(final View v, boolean flag) {
        v.setEnabled(flag);

    }
    
    public static String getTextViewStr(TextView tv) {
        return tv.getText().toString().trim();

    }
    
    public static void hiddenKeyboard( Activity activity) {
        try {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

            if (activity.getCurrentFocus() != null) {
                if (activity.getCurrentFocus().getWindowToken() != null) {
                    imm.hideSoftInputFromWindow(activity
                            .getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void hiddenKeyBoardByClick(Activity activity, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            BasicUiUtils.hiddenKeyboard(activity);
        }
    }





    /*public static void expandViews(final View v) {
        v.measure(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        final int targtetHeight = v.getMeasuredHeight();

        v.getLayoutParams().height = 0;
        v.setVisibility(View.VISIBLE);
        Animation a = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                v.getLayoutParams().height = interpolatedTime == 1
                        ? RelativeLayout.LayoutParams.WRAP_CONTENT
                        : (int)(targtetHeight * interpolatedTime);
                v.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        a.setDuration((int)(targtetHeight / v.getContext().getResources().getDisplayMetrics().density));
        v.startAnimation(a);
    }*/

    /*public static void collapseViews(final View v) {
        final int initialHeight = v.getMeasuredHeight();

        Animation a = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if(interpolatedTime == 1){
                    v.setVisibility(View.GONE);
                }else{
                    v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                    v.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density)*1);
        v.startAnimation(a);
    }*/
   /* public class DropDownAnim extends Animation {
        private final int targetHeight;
        private final View com.wondersgroup.insurance.lookafter.view;
        private final boolean down;

        public DropDownAnim(View com.wondersgroup.insurance.lookafter.view, int targetHeight, boolean down) {
            this.com.wondersgroup.insurance.lookafter.view = com.wondersgroup.insurance.lookafter.view;
            this.targetHeight = targetHeight;
            this.down = down;
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            int newHeight;
            if (down) {
                newHeight = (int) (targetHeight * interpolatedTime);
            } else {
                newHeight = (int) (targetHeight * (1 - interpolatedTime));
            }
            com.wondersgroup.insurance.lookafter.view.getLayoutParams().height = newHeight;
            com.wondersgroup.insurance.lookafter.view.requestLayout();
        }

        @Override
        public void initialize(int width, int height, int parentWidth,
                               int parentHeight) {
            super.initialize(width, height, parentWidth, parentHeight);
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    }*/



    /**
     * 测量HeadView宽高(注意：此方法仅适用于LinearLayout，请读者自己测试验证。)
     *	measureLinearLayout(mHeadView);
     * mHeadViewHeight = mHeadView.getMeasuredHeight();
     *  mHeadViewWidth = mHeadView.getMeasuredWidth();
     */
   /* public void measureLinearLayout(View pChild) {
        ViewGroup.LayoutParams p = pChild.getLayoutParams();
        if (p == null) {
            p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
        int lpHeight = p.height;

        int childHeightSpec;
        if (lpHeight > 0) {
            childHeightSpec = View.MeasureSpec.makeMeasureSpec(lpHeight,
                    View.MeasureSpec.EXACTLY);
        } else {
            childHeightSpec = View.MeasureSpec.makeMeasureSpec(0,
                    View. MeasureSpec.UNSPECIFIED);
        }
        pChild.measure(childWidthSpec, childHeightSpec);
    }*/
  /*  方法一：
    
    int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
    int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
    ssidtext.measure(w, h);
    int width =ssidtext.getMeasuredWidth();
    int height =ssidtext.getMeasuredHeight();

方法二：

    ViewTreeObserver vto = ssidtext.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
            int height = ssidtext.getMeasuredHeight();
            int width = ssidtext.getMeasuredWidth();
            return true;
        }
    });
*/


    /**
     * 设置背景
     * @param v
     * @param drawable
     */
    public static void  setBackgroudDrawable(View v, Drawable drawable){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            v.setBackground(drawable);
        }else {
           v.setBackgroundDrawable(drawable);
        }

    }



  /*  //应用程序App区域宽高等尺寸获取  减去 statusbar 高度
    Rect rect = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

    //获取状态栏高度
    Rect rect= new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
    int statusBarHeight = rectangle.top;

    //View布局区域宽高等尺寸获取
    Rect rect = new Rect();
    getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rect);

    上面这些方法最好在Activity的onWindowFocusChanged ()方法或者之后调运，因为只有这时候才是真正的显示OK*/
}
